package name.jiaosha.air.company.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.company.domain.Storage;
import name.jiaosha.air.company.domain.StorageSpace;
import name.jiaosha.air.company.service.ICompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.PageInfo;

@Controller
@RequestMapping("/admin/com")
public class StorageController {
	@Autowired
	private ICompanyService companyService;

	@RequestMapping("/createStorage")
	public ModelAndView save(@RequestParam String name,
			@RequestParam String address) {

		if (!AppConstants.DEBUG) {

		} else {

			Storage storage = new Storage();
			storage.setName(name.trim());
			storage.setAddress(address.trim());
			storage.setComId(AppConstants.comId);
			companyService.createStorage(storage);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/updateStorage")
	public ModelAndView update(@RequestParam int id, @RequestParam String name,
			@RequestParam String address) {

		if (!AppConstants.DEBUG) {

		} else {

			Storage storage = companyService.getStorageById(id);
			if (storage != null) {
				storage.setName(name.trim());
				storage.setAddress(address.trim());
				companyService.updateStorage(storage);

			}

		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

	public Storage getStorageById(int id) {
		Storage storage = companyService.getStorageById(id);
		// storage.getStorageSpaces();
		return storage;
	}

	@RequestMapping("/storageList")
	public ModelAndView getMyStorageList() throws Exception {

		PageInfo<Storage> pageInfo;
		if (!AppConstants.DEBUG) {
			pageInfo = new PageInfo<Storage>();
			List<Storage> list = new ArrayList<Storage>();
			for (int i = 1; i <= 22; i++) {
				Storage storage = new Storage();
				storage.setId(i);
				storage.setName("仓库" + i);
				storage.setAddress("杭州天堂软件园" + i);
				list.add(storage);
			}
			pageInfo.setItemList(list);

		} else {
			pageInfo = companyService.getStorageByComId(AppConstants.comId, 1,
					AppConstants.DEFAULT_SIZE);
		}
		ModelMap model = new ModelMap();
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/getStorageSpaces")
	public ModelAndView getStorageSpaces() throws Exception {

		Set<StorageSpace> storageSpaces = null;
		if (AppConstants.DEBUG) {
			storageSpaces = new HashSet<StorageSpace>();
			for (int i = 1; i <= 15; i++) {
				StorageSpace space = new StorageSpace();
				space.setId(i);
				space.setName("在左边" + i);
				storageSpaces.add(space);
			}

		} else {

		}
		ModelMap model = new ModelMap();
		model.addAttribute("root", storageSpaces);
		return new ModelAndView("jsonView", model);
	}

}
